<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CMU的WebSocket测试</title>
</head>
<body>
    <h1>WebSocket</h1>
</body>
<script>
    var websocket = null;
    if('WebSocket' in window) {
        websocket = new WebSocket('ws://127.0.0.1:8080/webSocket');//注意带context-path
        console.log('websocket创建完毕');
    }else {
        alert('error');
    }

    websocket.onopen = function(event) {
        console.log('建立连接');
    }

    websocket.onclose = function(event) {
        console.log('关闭连接');
    }

    websocket.onmessage = function(event) {
        console.log('收到消息：' + event.data);
        alert(event.data);
    }

    websocket.onerror = function(event){
        console.log('WebSocket发生错误！');
    }
</script>
</html>